package mo.daliy;

import java.util.HashMap;
import java.util.Map;

public class L904 {

    public int totalFruit(int[] fruits) {
        int n = fruits.length;
        int left = 0, right = 0;
        Map<Integer, Integer> map = new HashMap<>();
        int res = 0;
        for (; right < n; right++) {
            map.put(fruits[right], map.getOrDefault(fruits[right], 0)+1);
            while (map.size() > 2) {
                map.put(fruits[left], map.get(fruits[left])-1);
                if (map.get(fruits[left]) == 0) {
                    map.remove(fruits[left]);
                }
                left++;
            }
            res = Math.max(res, right-left+1);
        }
        return res;
    }

    public static void main(String[] args) {
        L904 test = new L904();
        System.out.println(test.totalFruit(new int[]{3,3,3,1,2,1,1,2,3,3,4}));
    }

}
